/**
 * Created by xpp on 2017/8/11.
 */
const gulp = require('gulp');
const clean = require('gulp-clean');
const replace = require('gulp-replace');
const changed = require('gulp-changed');
const uglify = require('gulp-uglify');
const filter = require('gulp-filter');
const sourcemaps = require('gulp-sourcemaps');
const babel = require('gulp-babel');
const source = require('vinyl-source-stream');
const gutil = require('gulp-util');
const sass = require('gulp-sass');

const webpack = require('webpack');
const webpackConfig = require('./webpack.config.js');


const config = {
    dev:{
        js:'./public/javascripts/',
        css:'./public/stylesheets/',
        img:'./public/images/'
    },
    build:{
        js:'./dist/javascripts',
        css:'./dist/stylesheets',
        img:'./dist/images'
    }
};


gulp.task('image', function () {
  return gulp.src([config.dev.img+'/style.scss'])
    .pipe(gulp.dest(config.build.img));
});



gulp.task("webpack", function(callback) {
  webpack(webpackConfig, function(err, stats) {
    if(err) throw new gutil.PluginError("webpack", err);
    gutil.log("[webpack]", stats.toString({
      // output options
    }));
    callback();
  });
});


gulp.task('css', function () {
  return gulp.src(config.dev.css+'/style.scss')
    .pipe(sourcemaps.init())
    .pipe(sass().on('error', sass.logError))
    .pipe(sourcemaps.write())
    .pipe(gulp.dest(config.build.css));
});



gulp.task('default', ['image','webpack','css']);

gulp.task('dev',['image','webpack','css'], ()=>{
    gulp.watch(config.dev.js+'**',['webpack']);
    gulp.watch(config.dev.css+'**',['css']);
});

